#include "loadscene.h"
#include "common.h"

using namespace cocos2d;

namespace fireplane
{
	CCScene* LoadingScene::scene()
	{
		CCScene * scene = NULL;
		do 
		{
			// 'scene' is an autorelease object
			scene = CCScene::create();
			CC_BREAK_IF(! scene);

			CCNode* parentNode = Common::createScaleNode(scene);
			// 'layer' is an autorelease object
			LoadingScene *layer = LoadingScene::node();
			CC_BREAK_IF(! layer);

			// add layer as a child to scene
			parentNode->addChild(layer);
		} while (0);

		// return the scene
		return scene;
	}

	bool LoadingScene::init()
	{
		CCLayer::init();

		CCSize winSize = Common::getWinSize();

		CCSprite* sprite1 = CCSprite::create("loading/loading1.jpg");
		sprite1->setPosition(ccp(winSize.width / 2,winSize.height / 2));
		addChild(sprite1);

		CCSprite* sprite0 = CCSprite::create("loading/loading0.jpg");
		sprite0->setPosition(ccp(winSize.width / 2,winSize.height / 2));
		addChild(sprite0);

		sprite0->runAction(CCRepeatForever::create(
			(CCActionInterval*)CCSequence::create(
			CCFadeOut::create(0.5f),
			CCFadeIn::create(0.5f),NULL)));


		scheduleUpdate();
		return true;
	}

	void LoadingScene::update( float dt )
	{
		if (_call)
		{
			(_call)();
		}
	}

	void LoadingScene::registerLoadFunction( do_load_logic call )
	{
		_call = call;
	}

	fireplane::do_load_logic LoadingScene::_call = NULL;

}